package com.zhenghaijin.animecontentservice.mapper;

import com.zhenghaijin.animecontentservice.model.Anime;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AnimeMapper {
    // 新增表创建方法注解
    @Update("CREATE TABLE IF NOT EXISTS anime (" +
            "id BIGINT PRIMARY KEY AUTO_INCREMENT," +
            "title VARCHAR(255)," +
            "description TEXT," +
            "category VARCHAR(50)," +
            "tags VARCHAR(100)," +
            "release_date DATE," +
            "cover_url VARCHAR(255))")
    void createTable();

    // 修改为注解方式查询
    @Select("SELECT id, title, description, category, tags, " +
            "release_date AS releaseDate, cover_url AS coverUrl FROM anime")
    List<Anime> findAll();

    @Select("SELECT id, title, description, category, tags, " +
            "release_date AS releaseDate, cover_url AS coverUrl FROM anime WHERE id = #{id}")
    Anime findById(Long id);

    @Insert("INSERT INTO anime (title, description, category, tags, release_date, cover_url) " +
            "VALUES (#{title}, #{description}, #{category}, #{tags}, #{releaseDate}, #{coverUrl})")
    int insert(Anime anime);

    @Update("UPDATE anime SET title = #{title}, description = #{description}, " +
            "category = #{category}, tags = #{tags}, release_date = #{releaseDate}, " +
            "cover_url = #{coverUrl} WHERE id = #{id}")
    int update(Anime anime);

    @Delete("DELETE FROM anime WHERE id = #{id}")
    int delete(Long id);
}